/**
 * Created by 周翔 on 2017/5/10.
 */

import * as types from './operationContentMutationTypes';


const mutations = {
  [types.OPERATION_CONTENT_QUERY_BEGIN](state) {
    state.operationContents = [];
  },
  [types.OPERATION_CONTENT_QUERY_SUC](state, {operationContents}) {
    state.operationContents = operationContents;
  },
  [types.OPERATION_CONTENT_ADD_SUC] (state, {operationContent}) {
    let operationContents = state.operationContents || [];
    operationContents.push(operationContent);
    state.operationContents = operationContents;
  },

  [types.OPERATION_CONTENT_UPDATE_SUC](state, {operationContent}) {
    for (let i = 0; i < state.operationContents.length; i++) {
      let item = state.operationContents[i];
      if (item.id === operationContent.id) {
        item.title = operationContent.title;
        item.subTitle = operationContent.subTitle;
        item.tag = operationContent.tag;
        item.style = operationContent.style;
        item.blockName = operationContent.blockName;
        item.priority = operationContent.priority;
        item.indicant = operationContent.indicant;
        item.url = operationContent.url;
        item.status = operationContent.status;
        item.createTime = operationContent.createTime;
        item.lastUpdatedTime = operationContent.lastUpdatedTime;
        item.indicantType = operationContent.indicantType;
        item.version = operationContent.version;
        item.description = operationContent.description;
        item.indicantDescription = operationContent.indicantDescription;
        item.extraMap = operationContent.extraMap;
        break;
      }
    }

  },

  [types.OPERATION_CONTENT_DELETE_BEGIN](state) {
  },

  [types.OPERATION_CONTENT_DELETE_SUC](state, {id}) {
    //从列表中移除userId
    for (let i = 0; i < state.operationContents.length; i++) {
      let item = state.operationContents[i];

      if (item.id === id) {
        console.log(`remove param[${id}] in state.operationContents`);
        state.operationContents.splice(i, 1);
        break;
      }
    }
  },
  [types.OPERATION_CONTENT_ONLINE_BEGIN](state, {operationContent}) {
    if (operationContent.status === '1') {
      operationContent.status = '0';
    } else {
      operationContent.status = '1';
    }

  },
  [types.OPERATION_CONTENT_ONLINE_SUC](state, {operationContent}) {
    for (let i = 0; i < state.operationContents.length; i++) {
      let item = state.operationContents[i];
      if (item.id === operationContent.id) {
        item.status = operationContent.status;
        break;
      }
    }
  }
}

export default mutations;
